package com.chen.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.back.entity.*;
import com.chen.back.mapper.*;
import com.chen.back.service.CourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chen
 * @since 2022-02-24
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private ChooseCourseMapper chooseCourseMapper;
    @Autowired
    private TermMapper termMapper;
    @Override
    public Map<String, Object> getThisCourses(Integer userId, Integer pageNum, Integer pageSize) {
       //默认的参数一页有5个数据
        int first = pageNum == null ? 1 : pageNum;
        int second = pageSize == null ? 5 : pageSize;
        PageHelper.startPage(first, second);

        //是可选的状态课程
        List<Course> courses = courseMapper.getThisCourses(userId);
        PageInfo pageInfo = new PageInfo(courses);
        Map<String, Object> map = new HashMap<>();
        //数据的总数
        map.put("total", pageInfo.getTotal());
        List<Map<String, Object>> list = new ArrayList<>();
        //for循环是根据课程的每一个id找到学院，老师，首先放到map中然后再进入list中
        for (Course course : courses) {
            Map<String, Object> map1 = new HashMap<>();
            //根据老师id找到老师
            User teacher = userMapper.selectById(course.getTeacherId());
            //根据学院的id找到学院
            School school = schoolMapper.selectById(course.getSchoolId());
            map1.put("teacher", teacher);
            map1.put("course", course);
            map1.put("school", school);
            list.add(map1);
        }
        map.put("list", list);
        return map;
    }

    @Override
    public boolean isContradict(Integer userId, Integer courseId) {
        List<Course> courses = courseMapper.getChooseCourseResult(userId);
        //如果课程id相等就是起冲突
        for (Course course : courses) {
            Course course1 = courseMapper.selectById(courseId);
            if (course.getCourseTime().equals(course1.getCourseTime())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int insertChooseCourse(Integer userId, Integer courseId) {
        return chooseCourseMapper.insertChooseCourse(userId, courseId);
    }

    @Override
    public Map<String, Object> getChooseCourseResult(Integer userId, Integer pageNum, Integer pageSize) {
        int first = pageNum == null ? 1 : pageNum;
        int second = pageSize == null ? 5 : pageSize;
        PageHelper.startPage(first, second);
        List<Course> courses = courseMapper.getChooseCourseResult(userId);
        PageInfo pageInfo = new PageInfo(courses);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        List<Map<String, Object>> list = new ArrayList<>();
        for (Course course : courses) {
            Map<String, Object> map1 = new HashMap<>();
            User teacher = userMapper.selectById(course.getTeacherId());
            School school = schoolMapper.selectById(course.getSchoolId());
            map1.put("teacher", teacher);
            map1.put("course", course);
            map1.put("school", school);
            list.add(map1);
        }
        map.put("list", list);
        return map;
    }

    //退课操作
    @Override
    public int deleteChooseCourse(Integer userId, Integer courseId) {
        return chooseCourseMapper.deleteChooseCourse(userId, courseId);
    }

    //得到所有的科目
    @Override
    public Map<String, Object> getHistoryCourses(Integer userId, Integer term, Integer pageNum, Integer pageSize) {
        int first = pageNum == null ? 1 : pageNum;
        int second = pageSize == null ? 5 : pageSize;
        PageHelper.startPage(first, second);
        if (term == 0) {
            term = null;
        }
        //根据用户的id与学期参数
        List<Course> courses = courseMapper.getHistoryCourses(userId, term);
        PageInfo pageInfo = new PageInfo(courses);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        List<Map<String, Object>> list = new ArrayList<>();
        for (Course course : courses) {
            Map<String, Object> map1 = new HashMap<>();
            User teacher = userMapper.selectById(course.getTeacherId());
            School school = schoolMapper.selectById(course.getSchoolId());
            map1.put("teacher", teacher);
            map1.put("course", course);
            map1.put("school", school);
            list.add(map1);
        }
        map.put("list", list);
        return map;
    }

    //得到本学期的成绩
    @Override
    public Map<String, Object> getThisGrade(Integer userId, Integer pageNum, Integer pageSize) {
        int first = pageNum == null ? 1 : pageNum;
        int second = pageSize == null ? 5 : pageSize;
        PageHelper.startPage(first, second);

        //根据用户的id+本学期的id为1查找到科目
        List<Course> courses = courseMapper.getThisCoursesHasGrade(userId);
        List<Map<String, Object>> maps = new ArrayList<>();
        for (Course course : courses) {
            Map<String, Object> map1 = new HashMap<>();
            //通过选择的科目循环得到id，就能选择出已选择的科目
            ChooseCourse chooseCourse = chooseCourseMapper.selectChooseCourseByCourseIdUserId(userId, course.getCourseId());
          //所有的科目（用户已经选择过的科目）
            map1.put("course", course);
            //本学期的科目
            map1.put("grade", chooseCourse);
            maps.add(map1);
        }
        PageInfo pageInfo = new PageInfo(courses);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("list", maps);
        return map;
    }

//    查看历史成绩
@Override
public Map<String, Object> getHistoryGrade(Integer userId, Integer term, Integer pageNum, Integer pageSize) {
    int first = pageNum == null ? 1 : pageNum;
    int second = pageSize == null ? 5 : pageSize;
    if (term == 0) {
        term = null;
    }
    PageHelper.startPage(first, second);
    List<Course> courses = courseMapper.getHistoryGrade(userId, term);
    List<Map<String, Object>> maps = new ArrayList<>();
    for (Course course : courses) {
        Map<String, Object> map1 = new HashMap<>();
        ChooseCourse chooseCourse = chooseCourseMapper.selectChooseCourseByCourseIdUserId(userId, course.getCourseId());
        map1.put("course", course);
        map1.put("grade", chooseCourse);
        maps.add(map1);
    }
    PageInfo pageInfo = new PageInfo(courses);
    Map<String, Object> map = new HashMap<>();
    map.put("total", pageInfo.getTotal());
    map.put("list", maps);
    return map;
}

//老师用户所查看本学期自己所要开的科目
@Override
public Map<String, Object> getThisStartCourses(Integer userId, Integer pageNum, Integer pageSize) {
    int first = pageNum == null ? 1 : pageNum;
    int second = pageSize == null ? 5 : pageSize;
    PageHelper.startPage(first, second);
    List<Course> courses = courseMapper.getThisStartCourses(userId);
    PageInfo pageInfo = new PageInfo(courses);
    Map<String, Object> map = new HashMap<>();
    map.put("total", pageInfo.getTotal());
    List<Map<String, Object>> list = new ArrayList<>();
    for (Course course : courses) {
        Map<String, Object> map1 = new HashMap<>();
        User teacher = userMapper.selectById(course.getTeacherId());
        School school = schoolMapper.selectById(course.getSchoolId());
        map1.put("teacher", teacher);
        map1.put("course", course);
        map1.put("school", school);
        list.add(map1);
    }
    map.put("list", list);
    return map;
}

//教师查看历史科目
@Override
public Map<String, Object> getHistoryStartCourses(Integer userId, Integer term, Integer pageNum, Integer pageSize) {
    int first = pageNum == null ? 1 : pageNum;
    int second = pageSize == null ? 5 : pageSize;
    PageHelper.startPage(first, second);
//    当学期为null时就包括所有的，此时term参数没有影响
    if (term == 0) {
        term = null;
    }
    List<Course> courses = courseMapper.getHistoryStartCourses(userId, term);
    PageInfo pageInfo = new PageInfo(courses);
    Map<String, Object> map = new HashMap<>();
    map.put("total", pageInfo.getTotal());
    List<Map<String, Object>> list = new ArrayList<>();
    for (Course course : courses) {
        Map<String, Object> map1 = new HashMap<>();
        User teacher = userMapper.selectById(course.getTeacherId());
        School school = schoolMapper.selectById(course.getSchoolId());
        map1.put("teacher", teacher);
        map1.put("course", course);
        map1.put("school", school);
        list.add(map1);
    }
    map.put("list", list);
    return map;
}

//学院教务处查看本学院中所有开课
@Override
public Map<String, Object> getALLStartCourses(Integer schoolId, Integer term, Integer pageNum, Integer pageSize) {
    int first = pageNum == null ? 1 : pageNum;
    int second = pageSize == null ? 5 : pageSize;
    PageHelper.startPage(first, second);
    if (term == 0) {
        term = null;
    }
    List<Course> courses = courseMapper.getAllStartCourses(schoolId, term);
    PageInfo pageInfo = new PageInfo(courses);
    Map<String, Object> map = new HashMap<>();
    map.put("total", pageInfo.getTotal());
    List<Map<String, Object>> list = new ArrayList<>();
    for (Course course : courses) {
        Map<String, Object> map1 = new HashMap<>();
        User teacher = userMapper.selectById(course.getTeacherId());
        School school = schoolMapper.selectById(course.getSchoolId());
        map1.put("teacher", teacher);
        map1.put("course", course);
        map1.put("school", school);
        list.add(map1);
    }
    map.put("list", list);
    return map;
}


//私有方法，得到所有开课科目

    private List<Course> getAllThisStartCourses(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("term_status", 1);
//        根据mybatis-plus进行条件构造得到状态为1学期，
        Term term = termMapper.selectOne(queryWrapper);
//根据找到学期，然后再用该学期的id为条件找到该学期下所有的科目
        List<Course> courses = courseMapper.getAllStartCourses(null, term.getTermId());
        return courses;
    }

    //开放选课
    @Override
    public void openChooseCourse() {
//根据上述的private方法得到本学期中所有科目
        List<Course> courses = getAllThisStartCourses();
//        通过for循坏再设置其状态为1，这样就能保住本学期中科目都处于可选的状态
        for (Course course : courses) {
            course.setCourseStatus(1);
            courseMapper.updateById(course);
        }
    }
//    关闭选课
@Override
public void closeChooseCourse() {
    List<Course> courses = courseMapper.selectList(null);
    for (Course course : courses) {
        course.setCourseStatus(0);
        courseMapper.updateById(course);
    }
}
}
